//
//  IWxChatMgrContact.m
//  WxSdk
//
//  Created by fusuwei on 14-9-18.
//  Copyright (c) 2014年 网信金融集团. All rights reserved.
//

#import "IWxChatMgrContact.h"
#import "ChatContactMgr.h"
#import "UcfgroupXMPPManger.h"
#import "PublicMacros.h"
#import "EPublicMacros.h"
#import "Utility.h"



@implementation IWxChatMgrContact
{
    
}


static IWxChatMgrContact *sharedContact;

static BOOL isAutoAcceptInvite;

+(IWxChatMgrContact*)sharedContact{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedContact=[[IWxChatMgrContact alloc] init];
        isAutoAcceptInvite = NO;
    });
    return sharedContact;
}



#pragma mark - 设置自动添加好友

/*!
 @method
 @brief         同步方法 设置是否自动接收添加好友的邀请
 @discussion
 @param accept  YES or NO 是否自动接收添加好友
 @result        是否设置成功
 */
- (void)setAutoAcceptNewContactInvite:(BOOL)accept
{
    isAutoAcceptInvite = accept;
    return;
}


/*!
 @method
 @brief         同步方法，获取本地所有好友联系人列表
 @result        返回ChatContact对象数组  为空：没有联系人
 */
- (NSMutableArray *)getMyAllLocalContacts;
{
    NSMutableArray *arryList = nil;
    
    arryList = [ChatContactMgr getAllContactList];
    
    return arryList;
}

/*!
 @method
 @brief         异步 请求获取我所有好友联系人
 @result        可以主动更新本地联系人数据
 */
- (void)requestMyAllContacts
{
    UcfgroupXMPPManger* delegate = [UcfgroupXMPPManger sharedInstance];
    if (![delegate isLogSuccess])
    {
        if (delegate.xmppContactDelegate && [delegate.xmppContactDelegate respondsToSelector:@selector(requestMyAllContactsRspState:errorState:)])
        {
            [delegate.xmppContactDelegate requestMyAllContactsRspState:Contact_Failure errorState:Contact_NotLogin];
        }
        return;
    }
    //---vince@2014-09-25
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *rosterVer = [defaults objectForKey:@"RosterVer"];
    if ((rosterVer == nil)|| [rosterVer isEqualToString:@""]) {
        rosterVer = @"0";
    }
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_queryRosterWithVer:rosterVer];
    return;
}

/*!
 @method
 @brief         同步方法，获取所有我已邀请的好友和邀请我的好友列表
 @result        返回 ChatContactRequest 对象数组  为空：没有我邀请的和邀请我的人
 */
- (NSMutableArray *)getAllInviteOrInvitedContacts
{
    NSMutableArray *arryList = nil;
    
    arryList = [ChatContactMgr getAllInviteOrInvitedContacts];
    
    return arryList;
}

/*!
 @method
 @brief         邀请好友
 @discussion    异步方法
 @param contactId 邀请好友的Id
 @param message  邀请好友时的附带信息
 @result        返回调用成功or失败
 */
- (void)inviteContact:(NSString *)contactId message:(NSString *)message delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    if (delegateUcf.xmppContactDelegate && [delegateUcf.xmppContactDelegate respondsToSelector:@selector(inviteContactRspState:contactId:errorState:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppContactDelegate inviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_NotLogin];
            return;
        }
        if (contactId == nil || [contactId isEqualToString:@""]) {
            [delegateUcf.xmppContactDelegate inviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_ContactIdNull];
            return;
        }
        if ([message length] > kMessageBodyMaxLen) {
            [delegateUcf.xmppContactDelegate inviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_MessageMaxLen_Error];
            return;
        }
    }
    
    NSString *jidNameValid = [Utility convertToJid:contactId];
    
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_AddFriendSubscribe:jidNameValid reqMsg:message];
    return;
}

/*!
 @method
 @brief     拒绝好友的邀请
 @discussion 同步方法
 @param contactId 要拒绝的好友Id
 @param reason    原因
 @result 
 */
- (void)rejectInviteContact:(NSString *)contactId reason:(NSString *)reason delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppContactDelegate = delegate;
    if (delegateUcf.xmppContactDelegate && [delegateUcf.xmppContactDelegate respondsToSelector:@selector(rejectInviteContactRspState:contactId:errorState:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppContactDelegate rejectInviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_NotLogin];
            return;
        }
        if (contactId == nil || [contactId isEqualToString:@""]) {
            [delegateUcf.xmppContactDelegate rejectInviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_ContactIdNull];
            return;
        }
        if ([reason length] > kMessageBodyMaxLen) {
            [delegateUcf.xmppContactDelegate rejectInviteContactRspState:Contact_Failure contactId:contactId errorState:Contact_MessageMaxLen_Error];
            return;
        }
    }
        
    NSString *jidNameValid = [Utility convertToJid:contactId];
    
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_rejectAgreeAddBuddy:jidNameValid reason:reason];
    //ChatContactRequest 将表中的ContactState更新为拒绝状态，没有删除为以后扩展
    [ChatContactMgr saveUpdateChatContactRequestContactId:contactId fieldName:@"ContactState" value:Rejected];
    return;
}

/*!
 @method
 @brief       从好友列表中删除某个好友
 @discussion  异步方法
 @param       contactId 要删除的好友Id
 @result      removeContactRsp:contactId:errorState:
 */
- (void)removeContact:(NSString *)contactId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppContactDelegate = delegate;
    if (delegateUcf.xmppContactDelegate && [delegateUcf.xmppContactDelegate respondsToSelector:@selector(removeContactRsp:contactId:errorState:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppContactDelegate removeContactRsp:Contact_Failure contactId:contactId errorState:Contact_NotLogin];
            return;
        }
        if (contactId == nil || [contactId isEqualToString:@""]) {
            [delegateUcf.xmppContactDelegate removeContactRsp:Contact_Failure contactId:contactId errorState:Contact_ContactIdNull];
            return;
        }
    }
    
    NSString *jidNameValid = [Utility convertToJid:contactId];
    
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_removeBuddy:jidNameValid];
    return;
}

#pragma mark - Remark buddy request
/*!
 @method
 @brief     修改某个好友备注信息
 @discussion
 @param     contactId 要修改备注的好友Id
 @param     remarkStr 修改的备注信息
 @result    remarkContactRsp:contactId:errorState:
 */
- (void)remarkContact:(NSString *)contactId remarkMsg:(NSString *)remarkStr delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppContactDelegate = delegate;
    if (delegateUcf.xmppContactDelegate && [delegateUcf.xmppContactDelegate respondsToSelector:@selector(remarkContactRsp:contactId:errorState:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppContactDelegate remarkContactRsp:Contact_Failure contactId:contactId errorState:Contact_NotLogin];
            return;
        }
        if (contactId == nil || [contactId isEqualToString:@""]) {
            [delegateUcf.xmppContactDelegate remarkContactRsp:Contact_Failure contactId:contactId errorState:Contact_ContactIdNull];
            return;
        }
        if ([remarkStr length] > kMessageBodyMaxLen) {
            [delegateUcf.xmppContactDelegate remarkContactRsp:Contact_Failure contactId:contactId errorState:Contact_RemarkStrMaxLen_Error];
            return;
        }
    }

    NSString *jidNameValid = [Utility convertToJid:contactId];
    //更新备注信息
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_setNickname:remarkStr forUserId:jidNameValid];
    //retval = [ChatContactMgr updateTheContact:contactId ContactName:remarkStr];
    return;
}


/*!
 @method
 @brief           接受某个好友发送的好友请求
 @discussion
 @param contactId 需要接受的好友Id
 @param pError    错误信息
 @result          acceptContactInviteRsp:contactId:errorState:
 */
- (void)acceptContactInvite:(NSString *)contactId delegate:(id)delegate
{
    UcfgroupXMPPManger* delegateUcf = [UcfgroupXMPPManger sharedInstance];
    
    delegateUcf.xmppContactDelegate = delegate;
    if (delegateUcf.xmppContactDelegate && [delegateUcf.xmppContactDelegate respondsToSelector:@selector(acceptContactInviteRsp:contactId:errorState:)])
    {
        if (![delegateUcf isLogSuccess])
        {
            [delegateUcf.xmppContactDelegate acceptContactInviteRsp:Contact_Failure contactId:contactId errorState:Contact_NotLogin];
            return;
        }
        if (contactId == nil || [contactId isEqualToString:@""]) {
            [delegateUcf.xmppContactDelegate acceptContactInviteRsp:Contact_Failure contactId:contactId errorState:Contact_ContactIdNull];
            return;
        }
    }
    
    NSString *jidNameValid = [Utility convertToJid:contactId];
    
    [[UcfgroupXMPPManger sharedInstance] ucfgroup_AgreeAddBuddy:jidNameValid];
    return;
}



- (void)registDelegate:(id<EWxChatMgrContactDelegate>)delegate
{
    WXLog(@"IWxChatMgrContact:registDelegate");
    [UcfgroupXMPPManger sharedInstance].xmppContactDelegate = delegate;
}

- (void)unregistDelegate:(id<EWxChatMgrContactDelegate>)delegate
{
    WXLog(@"IWxChatMgrContact:unregistDelegate");
    [UcfgroupXMPPManger sharedInstance].xmppContactDelegate = nil;
}

/*!
 @method
 @brief           获取联系人对象
 @discussion
 @param contactId 要获取的好友Id
 @result          返回ChatContact对象或nil
 */
- (ChatContact *)fetchChatContactByContactId:(NSString *)contactId
{
    if (contactId == nil || [contactId isEqualToString:@""]) {
        return nil;
    }
    
    ChatContact *getContact = [ChatContactMgr fetchChatContactByContactId:contactId];
    return getContact;
}


@end
